package com.copm.ifm.servers.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.constant.DbConstants;
import com.copm.ifm.base.basic.exception.ServiceException;
import com.copm.ifm.base.basic.pojo.DictContent;
import com.copm.ifm.base.basic.pojo.DictContentList;
import com.copm.ifm.base.basic.pojo.vo.DictContentVO;
import com.copm.ifm.base.basic.pojo.vo.DictContentVoList;
import com.copm.ifm.base.basic.util.AuthUtil;
import com.copm.ifm.base.service.mp.page.PageProxy;
import com.copm.ifm.servers.sys.dto.SysDictPageDTO;
import com.copm.ifm.servers.sys.entity.SysBrand;
import com.copm.ifm.servers.sys.entity.SysDict;
import com.copm.ifm.servers.sys.mapper.SysDictMapper;
import com.copm.ifm.servers.sys.service.SysDictService;
import com.copm.ifm.servers.sys.vo.SysDictPageVO;
import com.copm.ifm.servers.sys.vo.SysDictVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 系统字典表 服务实现类
 *
 * @author zzf
 * @since 2020-07-15
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    @Resource
    SysDictMapper sysDictMapper;

    @Override
    public PageVO<SysDictPageVO> selectForPage(PageDTO<SysDictPageDTO> pageDTO) {
        return PageProxy.proxy(baseMapper::selectForPage, pageDTO);
    }

    @Override
    public SysDictVO selectById(int id, int language) {
        SysDict sysDict = baseMapper.selectById(id);
        return this.do2dto(sysDict, language);
    }

    @Override
    public SysDictVO selectByCode(String code, int language) {
        SysDict sysDict = baseMapper.selectOne(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getCode, code)
        );
        return this.do2dto(sysDict, language);
    }

    @Override
    public List<SysDict> listAll() {
        return baseMapper.selectList(new LambdaQueryWrapper<SysDict>()
                .select(SysDict::getId,
                        SysDict::getCode,
                        SysDict::getContent
                )
        );
    }

    @Override
    public SysDict updateSys(DictContent entity, String code) {
        SysDict sysDict = sysDictMapper.selectOne(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getCode, code)
        );
        DictContentList content = sysDict.getContent();
        Map<Integer, DictContent> collect =
                content.stream().collect(Collectors.toMap(DictContent::getKey, Function.identity()));
        if (collect.containsKey(entity.getKey())) {
            throw new ServiceException("新增的编码已存在，请修改后提交");
        }
        content.add(entity);
        SysDict sysDictNew = new SysDict();
        BeanUtils.copyProperties(sysDict, sysDictNew);
        sysDictNew.setContent(content);
        return sysDictNew;
    }

    @Override
    public void deletedByKey(Integer key, String code) {
        SysDict sysDict = sysDictMapper.selectOne(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getCode, code)
        );
        ArrayList<DictContent> content = sysDict.getContent();
        Map<Integer, DictContent> collect =
                content.stream().collect(Collectors.toMap(DictContent::getKey, Function.identity()));
        collect.remove(key);
        DictContentList dictList = new DictContentList();
        collect.forEach((k, v) -> {
            dictList.add(v);
        });
        sysDict.setContent(dictList);
        sysDictMapper.updateById(sysDict);
    }

    @Override
    public SysDictVO selectByName(String name, int language) {
        SysDict sysDict = baseMapper.selectOne(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getName, name)
        );
        return this.do2dto(sysDict, language);
    }

    @Override
    public void updateEntityById(SysDict entity) {
        entity.setUpdateBy(AuthUtil.getCurrentUserId());
        entity.setUpdateTime(new Date());
        updateById(entity);
    }

    @Override
    public int updateDeleted(int id) {
        SysDict sysDict = new SysDict();
        sysDict.setId(id);
        sysDict.setDeleted(DbConstants.DEFAULT_YES);
        return sysDictMapper.updateById(sysDict);
    }

    /**
     * 将DO类转成DTO类
     *
     * @author zzf
     * @date 17:20 2020/7/20
     */
    private SysDictVO do2dto(SysDict dict, int language) {
        SysDictVO sysDictVO = new SysDictVO();
        sysDictVO.setId(dict.getId());
        sysDictVO.setCode(dict.getCode());
        //多语言转化
        sysDictVO.setName(AuthUtil.invokeByLanguage(language, dict::getName, dict::getFtName, dict::getEnName));
        DictContentVoList contentList = new DictContentVoList();
        //内容处理
        List<DictContent> dictContentList = dict.getContent();
        if (dictContentList != null && dictContentList.size() > 0) {
            for (DictContent s : dictContentList) {
                DictContentVO content = new DictContentVO();
                content.setKey(s.getKey());
                content.setEnabled(s.getEnabled());
                content.setOrderNum(s.getOrderNum());
                //多语言转化
                content.setValue(AuthUtil.invokeByLanguage(language, s::getValue, s::getFtValue, s::getEnValue));
                contentList.add(content);
            }
        }
        sysDictVO.setContent(contentList);
        return sysDictVO;
    }

    @Override
    public String getName(String code, Integer key){
        SysDictVO sysDictVO = selectByCode(code, 1);
        ArrayList<DictContentVO> content = sysDictVO.getContent();
        Map<Integer, String> map = content.stream()
                .collect(Collectors.toMap(DictContentVO::getKey, DictContentVO::getValue));
        return map.get(key);
    }

}
